package com.nike.amoeba.ui.frg

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.LogUtils
import com.kingja.loadsir.core.LoadService
import com.nike.amoeba.R
import com.nike.amoeba.api.UIHelper
import com.nike.amoeba.base.BaseFragment
import com.nike.amoeba.databinding.FStockManageBinding
import com.nike.amoeba.ext.WAREHOUSE_CHU_TYPE
import com.nike.amoeba.ext.WAREHOUSE_ORDER_TYPE
import com.nike.amoeba.ext.WAREHOUSE_RU_TYPE
import com.nike.amoeba.ext.bindViewPager2
import com.nike.amoeba.ext.init
import com.nike.amoeba.mar.eventViewModel
import com.nike.amoeba.viewmodel.StockManageModel

/**
 * User: Nike
 *  2024/5/8 09:52
 *      库存出入库父类
 */
class StockManageParentFrg : BaseFragment<StockManageModel, FStockManageBinding>() {

    lateinit var loadsir: LoadService<Any>

    var fragments: ArrayList<Fragment> = arrayListOf()

    val stockManageModel: StockManageModel by activityViewModels()

    override fun initView(savedInstanceState: Bundle?) {
        arguments?.let {
            mViewModel.title.set(it.getString("title"))
            mViewModel.code.set(it.getString("code"))

            onUploadFrg()

            mDatabind.includeSearch.ivQr.setOnClickListener {
                UIHelper.startZxingAct(WAREHOUSE_ORDER_TYPE)
            }

            mDatabind.includeSearch.etText.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                }

                override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                    stockManageModel.searchText.value = p0.toString()
                }

                override fun afterTextChanged(p0: Editable?) {
                }
            })
        }

        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onResume(owner: LifecycleOwner) {
                super.onResume(owner)
                setToolBarTitle(mViewModel.title.get())
            }
        })
    }

    override fun createObserver() {
        super.createObserver()
        eventViewModel.stockManageUploadEvent.observeInFragment(this, Observer {
            fragments.clear()
            mViewModel.title.set(it.Title)
            mViewModel.code.set(it.Code)
            setToolBarTitle(mViewModel.title.get())
            onUploadFrg()
        })
    }

    private fun onUploadFrg() {
        mViewModel.code.get()?.let {
            val ruFrg = StockManageFrg().apply {
                arguments = Bundle().apply {
                    putInt("type", WAREHOUSE_RU_TYPE)
                    putString("code", it)
                }
            }
            fragments.add(ruFrg)

            val chuFrg = StockManageFrg().apply {
                arguments = Bundle().apply {
                    putInt("type", WAREHOUSE_CHU_TYPE)
                    putString("code", it)
                }
            }
            fragments.add(chuFrg)

            mDatabind.includeViewpager.viewPager.init(this, fragments)
            var mTitles =
                arrayListOf(
                    getString(R.string.warehouse),
                    getString(R.string.out_warehouse)
                )

            mDatabind.includeViewpager.magicIndicator.bindViewPager2(mDatabind.includeViewpager.viewPager, mTitles)
            mDatabind.includeViewpager.viewPager.offscreenPageLimit = mTitles.size
        }
    }

}